package com.xm.queue.task;

public class TestTask4 {
    public static void main(String[] args) {
        // 按电脑配置初始化消费者工人数量
        Consumer.init();

        int times = 50;
        int batch = 100;
        for (int i = 0; i < times; i++) {
            try {
                new Thread(() -> {
//                    Producer.make(batch);
                    Producer.makeNew(batch);
//                    Producer.makePutAlways(batch);
//                    Producer.makeOfferAlways(batch);

                }).start();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("\nProgress：实际消费任务/每秒支持消费任务 队列任务/实际接收任务 实际延迟接收任务");

        // 模拟消费进度
        long start = System.currentTimeMillis();
        int realPollCount;
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            try {
                Thread.sleep(10L);
                realPollCount = TaskUtil.realPollCount.intValue();
                // 使用 \r 覆盖当前行，不换行
                System.out.printf("\rProgress：%d/%d %d/%d %d", realPollCount, TaskUtil.LIMIT, TaskUtil.QUEUE.size(), TaskUtil.realPutCount.intValue(), TaskUtil.realPutDelayCount.intValue());
//                System.out.println("size = " + TaskUtil.QUEUE.size());

                // TEST
//                if (realPollCount == times*batch) {
//                    System.out.println(TaskUtil.realPutDelayCount.intValue());
//                }

                if(TaskUtil.count() == times*batch && TaskUtil.QUEUE.isEmpty()
                        && realPollCount == TaskUtil.count()) {
                    System.out.println();
                    break;
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("end queue size = " + TaskUtil.QUEUE.size());
        System.out.println("end predict count = " + TaskUtil.count());
        System.out.println("end realPutCount = " + TaskUtil.realPutCount.intValue());
        System.out.println("end realPutDelayCount = " + TaskUtil.realPutDelayCount.intValue());
        System.out.println("end realPollCount = " + TaskUtil.realPollCount.intValue());
        System.out.println("耗时 = " + (System.currentTimeMillis() - start) + "ms");
        // 关闭线程池
        TaskUtil.shutDown();
    }
}
